<?php require VIEWPATH. ADMIN_DIR. DIRECTORY_SEPARATOR. 'header.php'?>
	<style type="text/css">
		.tree-branch-name .fa-check{background-color: #F9A021;border-color: #F9A021;color: #FFF;width: 15px;height: 15px;line-height: 15px;font-size: 11px;text-align: center;border-radius: 3px;position: relative;bottom: 1px;margin-right: 5px}
	</style>
	<div class="page-header">
		<h1><?=$teacherId ? '编辑教师信息' : '新增教师'?></h1>
	</div>
	<div class="col-xs-12">
		<form class="form-horizontal" id="teacher-form">
			<input type="hidden" name="id" value="<?=$teacherInfo['id']?>" />
			<input type="hidden" name="role_id_set" value="<?=$teacherInfo['role_id_set']?>" />
			<input type="hidden" name="headimgurl" value="<?=$teacherInfo['headimgurl']?>" />
			<input type="hidden" name="faceimage" value="<?=$teacherInfo['face_image']?>" />
			<div class="form-group">
				<label class="col-sm-3 control-label no-padding-right">教师名称：</label>
				<div class="col-sm-6">
					<input class="form-control" type="text" name="teacher_name" placeholder="请填写教师名称" value="<?=$teacherInfo['realname']?>"
						<?php if($teacherId && $teacherInfo['x_teacher_id']): ?>
							readonly 
						<?php endif ?>
					/>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label no-padding-right">性别：</label>
				<div class="col-sm-6">
					<select class="form-control" name="gender">
						<option value="男">男</option>
						<option value="女">女</option>
					</select>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label no-padding-right">登录账号：</label>
				<div class="col-sm-6">
					<input class="form-control" type="text" name="username" placeholder="请填写教师登录账号" value="<?=$teacherInfo['username']?>" 
						<?php if($teacherId && $teacherInfo['x_teacher_id']): ?>
							readonly 
						<?php endif ?>
					/>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label no-padding-right">
					<a href="javascript:;" data-rel="tooltip" data-original-title="教师头像，用于班牌和手机端展示"><i class="fa fa-question-circle"></i></a> 头像上传：
				</label>
				<div class="col-sm-6">
					<a href="javascript:;" id="upload-img">
						<?php if($teacherInfo['headimgurl']): ?>
							<img class="fit-cover" src="<?=$teacherInfo['headimgurl']?>" width="160" height="160" />
						<?php else: ?>
							<img class="fit-cover" src="<?=$staticAdminUrl?>assets/images/upload.png" width="160" height="160" />
						<?php endif;?>
					</a>
				</div>
			</div>

			<div class="form-group">
				<label class="col-sm-3 control-label no-padding-right">
					<a href="javascript:;" data-rel="tooltip" data-original-title="教师人脸图像，用于教师考勤人脸识别"><i class="fa fa-question-circle"></i></a> 人脸图像：
				</label>
				<div class="col-sm-6">
					<?php if($teacherInfo['face_image']): ?>
						<a href="javascript:;" id="upload-img-face">
							<img class="fit-cover" src="<?=$teacherInfo['face_image']?>" width="160" height="160" />
						</a>
					<?php else: ?>
						<a href="javascript:;" id="upload-img-face">
							<img class="fit-cover" src="<?=$staticAdminUrl?>assets/images/upload.png" width="160" height="160" />
						</a>
					<?php endif; ?>
				</div>
			</div>

			<!-- <div class="form-group">
				<label class="col-sm-3 control-label no-padding-right">任课班级：</label>
				<div class="col-sm-6">
					<?php foreach($gradeArray as $key => $grade): ?>
						<div class="well well-sm">
							<a class="blue select-grade" href="javascript:;" title="全选年级"><?=$key?> <span style="color: #999;margin-left: 5px">(点击选中全部班级)</span></a>
							<div class="mt5">
								<?php foreach($grade['class'] as $class): ?>
									<label class="inline checkbox-item mr20">
										<input class="ace" type="checkbox" name="teacher_class" value="<?=$class['id']?>" />
										<span class="lbl"> <?=$class['class_name']?></span>
									</label>
								<?php endforeach; ?>
							</div>
						</div>
					<?php endforeach; ?>
				</div>
			</div> -->
			<div class="form-group">
				<label class="col-sm-3 control-label no-padding-right">联系电话：</label>
				<div class="col-sm-6">
					<input class="form-control" type="text" name="mobile" placeholder="请填写教师联系电话" value="<?=$teacherInfo['mobile']?>" />
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label no-padding-right">所属角色：</label>
				<div class="col-sm-6">
					<ul id="role-tree"></ul>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label no-padding-right">教师风采（班主寄语）：</label>
				<div class="col-sm-6">
					<textarea class="pd0" id="content" name="content" style="height: 400px;width: 100%"><?=urldecode($teacherInfo['content'])?></textarea>
				</div>
			</div>
			<div class="clearfix">
				<div class="col-md-offset-3 col-md-9">
					<button class="btn btn-info" type="submit">
						<i class="ace-icon fa fa-check"></i> 提交
					</button>
				</div>
			</div>
		</form>
	</div>
<?php require VIEWPATH. ADMIN_DIR. DIRECTORY_SEPARATOR. 'footer.php'?>
<!-- 百度编辑器 -->
<script src="<?=$staticAdminUrl?>assets/ueditor/ueditor.config.js"></script>
<script src="<?=$staticAdminUrl?>assets/ueditor/ueditor.all.js"></script>
<script type="text/javascript">
	$(function(){
		var roleTree = $("#role-tree");
		var uploadTarget = $("#upload-img");
		var uploadTargetFace = $("#upload-img-face");
		var form = $("#teacher-form");
		// 调用角色控件
		select_role(<?=$roleDatasource?>);
		// 选择角色
		<?php if($teacherInfo && $teacherInfo['role_id_set']): ?>
			<?php $roleArray = explode(",", $teacherInfo['role_id_set']); ?>
			<?php foreach($roleArray as $role): ?>
				roleTree.tree('selectItem', roleTree.find("#<?=$role?>"));
			<?php endforeach; ?>
		<?php endif; ?>

		// 修改教师
		<?php if($teacherInfo): ?>
			$("[name=gender]").val("<?=$teacherInfo['gender']?>");
		<?php endif; ?>

		// 初始化编辑器
		var ue = UE.getEditor('content');

		// 初始化头像上传插件
		setTimeout(function(){
			uploadTarget.uploader({
				"server":  '<?=UP_FORM_URL?>',
				"authorization": "<?=$signature?>",
				"policy": "<?=$policy?>",
				"callback": function(response) {
					var imgFile = "<?=UP_URL?>" + response.url;
					uploadTarget.find("img").attr("src", imgFile);
					form.find("[name=headimgurl]").val(imgFile);
				}
			});
		}, 200)

		// 初始化人脸上传插件
		setTimeout(function(){
			uploadTargetFace.uploader({
				"server":  '<?=UP_FORM_URL?>',
				"authorization": "<?=$signature?>",
				"policy": "<?=$policy?>",
				"callback": function(response) {
					var imgFile = "<?=UP_URL?>" + response.url;
					uploadTargetFace.find("img").attr("src", imgFile);
					form.find("[name=faceimage]").val(imgFile);
				}
			});
		}, 200)

		// 提交表单
		var submitUrl = "<?=admin_url('school/save_teacher_action')?>";
		form_submit(form, submitUrl, function(res) {
			tips_alert(res.msg, res.success, function(){
				if(res.success) {
					window.location.href = "<?=admin_url('school/teacher')?>";
				}
			});
		})
		// 任课班级选择
		<?php if(isset($teacherInfo) && !empty($teacherInfo) && !empty($teacherInfo['class_ids'])): ?>
			// 选中接收的班级
			var classArray = "<?=$teacherInfo['class_ids']?>".split(",");
			for(var i in classArray) {
				var classTeacher = $("[name=teacher_class][value="+ classArray[i] +"]")[0];
				if(classTeacher) {
					classTeacher.checked = true;
				}
			}
		<?php endif; ?>

		// 点击年级，全选下级班级
		var cancel = false;
		$(".select-grade").click(function(){
			// 第一选择，第二次取消选择
			if(cancel) {
				// 取消选择
				$(this).parent().find("[name=teacher_class]").each(function(){
					$(this)[0].checked = false;
				});
				cancel = ! cancel;
			} else {
				// 选择
				$(this).parent().find("[name=teacher_class]").each(function(){
					$(this)[0].checked = true;
				});
				cancel = ! cancel;
			}
		})
	})
</script>